package com.yueyang.sojourn.server.controller;

import com.yueyang.sojourn.server.model.enums.*;
import com.yueyang.sojourn.server.model.vo.EnumVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yemingxing
 * @summary
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-13 22:30
 */
@Api(tags = "枚举 API")
@RestController
public class EnumController extends BaseController {

    private final static Map<String, List<EnumVo>> ENUM_MAP = new HashMap<>();

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "项目所有枚举")
    @GetMapping("/sojourn/enums/all")
    public Map<String, List<EnumVo>> allEnums() {
        return ENUM_MAP;
    }

    @PostConstruct
    public void initMap() {
        ENUM_MAP.put(getEnumKey(BasicFacilityEnum.class), getEnumVos(BasicFacilityEnum.values()));
        ENUM_MAP.put(getEnumKey(RecreationEnum.class), getEnumVos(RecreationEnum.values()));
        ENUM_MAP.put(getEnumKey(CateringEnum.class), getEnumVos(CateringEnum.values()));
        ENUM_MAP.put(getEnumKey(OperStatusEnum.class), getEnumVos(OperStatusEnum.values()));
        ENUM_MAP.put(getEnumKey(StatusEnum.class), getEnumVos(StatusEnum.values()));
        ENUM_MAP.put(getEnumKey(MerchSaleTypeEnum.class), getEnumVos(MerchSaleTypeEnum.values()));
        ENUM_MAP.put(getEnumKey(ShelfStatusEnum.class), getEnumVos(ShelfStatusEnum.values()));
        ENUM_MAP.put(getEnumKey(BannerShowTypeEnum.class), getEnumVos(BannerShowTypeEnum.values()));
        ENUM_MAP.put(getEnumKey(JumpTypeEnum.class), getEnumVos(JumpTypeEnum.values()));
        ENUM_MAP.put(getEnumKey(LayoutTypeEnum.class), getEnumVos(LayoutTypeEnum.values()));
    }

    /**
     * 生成key，传给前端的key值，首字母小写
     *
     * @param iEnumClass 枚举类
     * @return String
     */
    private String getEnumKey(Class<? extends IEnum> iEnumClass) {
        char[] chars = iEnumClass.getSimpleName().substring(0, iEnumClass.getSimpleName().length() - 4).toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

    /**
     * 枚举转换EnumVo List
     *
     * @param iEnums 枚举值
     * @return List<EnumVo>
     */
    private List<EnumVo> getEnumVos(IEnum[] iEnums) {
        return Arrays.stream(iEnums).map(iEnum -> new EnumVo(iEnum.getCode(), iEnum.getDesc())).collect(Collectors.toList());
    }
}
